package org.seven.jrdp.biz.t.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.seven.jrdp.biz.t.mapper.TYsMapper;
import org.seven.jrdp.biz.t.model.TYs;
import org.seven.jrdp.commons.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

@Service
public class TYsService {
	@Autowired
	private TYsMapper tYsMapper;

	@Transactional(readOnly = true)
	public List<TYs> find() {
		PageHelper.orderBy("cx_");
		return tYsMapper.selectAll();
	}

	@Transactional
	public void save(List<TYs> list) {
		tYsMapper.deleteAll();
		for (int i = 0; i < list.size(); i++) {
			TYs tYs = list.get(i);
			tYs.setYs(tYs.getYs().trim());
			if (null != tYsMapper.selectByPrimaryKey(tYs.getYs())) {
				throw new BizException(String.format("第%d行，元素不允许重复", i + 1));
			}
			try {
				if (StringUtils.isNotEmpty(tYs.getSx())) {
					Double.valueOf(tYs.getSx());
				}
			} catch (Exception e) {
				throw new BizException(String.format("第%d行，上限格式错误，请重新输入", i + 1));
			}
			try {
				if (StringUtils.isNotEmpty(tYs.getXx())) {
					Double.valueOf(tYs.getXx());
				}
			} catch (Exception e) {
				throw new BizException(String.format("第%d行，下限格式错误，请重新输入", i + 1));
			}
			try {
				if (StringUtils.isNotEmpty(tYs.getPc())) {
					Double.valueOf(tYs.getPc());
				}
			} catch (Exception e) {
				throw new BizException(String.format("第%d行，偏差格式错误，请重新输入", i + 1));
			}
			tYsMapper.insertSelective(tYs);
		}
	}
}